## Will start with importing data
import pandas as pd
# We will begin by loading a data file for Atwater Village that details the structures built in the area.
# note the relative filepath! where is this file located?
df = pd.read_csv('data/Atwater2017_HHIncomeCategories.csv')
df = pd.read_csv(
'data/Atwater2017_HHIncomeCategories.csv',
dtype=
{
'Geo_FIPS':str,
'Geo_STATE':str,
'Geo_COUNTY': str
}
)
df.columns[df.isna().all()].tolist()
['Geo_US', 'Geo_REGION', 'Geo_DIVISION', 'Geo_STATECE', 'Geo_COUSUB', 'Geo_PLACE', 'Geo_PLACESE', 'Geo_CONCIT', 'Geo_AIANHH', 'Geo_AIANHHFP', 'Geo_AIHHTLI', 'Geo_AITSCE', 'Geo_AITS', 'Geo_ANRC', 'Geo_CBSA', 'Geo_CSA', 'Geo_METDIV', 'Geo_MACC', 'Geo_MEMI', 'Geo_NECTA', 'Geo_CNECTA', 'Geo_NECTADIV', 'Geo_UA', 'Geo_UACP', 'Geo_CDCURR', 'Geo_SLDU', 'Geo_SLDL', 'Geo_VTD', 'Geo_ZCTA3', 'Geo_ZCTA5', 'Geo_SUBMCD', 'Geo_SDELM', 'Geo_SDSEC', 'Geo_SDUNI', 'Geo_UR', 'Geo_PCI', 'Geo_TAZ', 'Geo_UGA', 'Geo_BTTR', 'Geo_BTBG', 'Geo_PUMA5', 'Geo_PUMA1']
df = df.dropna(axis=1,how="all")
df.head()
| Geo_FIPS | Geo_GEOID | Geo_NAME | Geo_QName | Geo_STUSAB | Geo_SUMLEV | Geo_GEOCOMP | Geo_FILEID | Geo_LOGRECNO | Geo_STATE | Geo_COUNTY | Geo_TRACT | Geo_BLKGRP | SE_B14001_001 | SE_B14001_002 | SE_B14001_003 | SE_B14001_004 | SE_B14001_005 | SE_B14001_006 | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 060371863011 | 15000US060371863011 | Block Group 1, Census Tract 1863.01, Los Angel... | Block Group 1, Census Tract 1863.01, Los Angel... | ca | 150 | 0 | ACSSF | 15232 | 06 | 037 | 186301 | 1 | 330 | 46 | 67 | 54 | 50 | 113 |
| 1 | 060371863012 | 15000US060371863012 | Block Group 2, Census Tract 1863.01, Los Angel... | Block Group 2, Census Tract 1863.01, Los Angel... | ca | 150 | 0 | ACSSF | 15233 | 06 | 037 | 186301 | 2 | 327 | 95 | 148 | 31 | 9 | 44 |
| 2 | 060371863013 | 15000US060371863013 | Block Group 3, Census Tract 1863.01, Los Angel... | Block Group 3, Census Tract 1863.01, Los Angel... | ca | 150 | 0 | ACSSF | 15234 | 06 | 037 | 186301 | 3 | 268 | 73 | 129 | 44 | 10 | 12 |
| 3 | 060371863021 | 15000US060371863021 | Block Group 1, Census Tract 1863.02, Los Angel... | Block Group 1, Census Tract 1863.02, Los Angel... | ca | 150 | 0 | ACSSF | 15235 | 06 | 037 | 186302 | 1 | 584 | 129 | 115 | 109 | 71 | 160 |
| 4 | 060371864011 | 15000US060371864011 | Block Group 1, Census Tract 1864.01, Los Angel... | Block Group 1, Census Tract 1864.01, Los Angel... | ca | 150 | 0 | ACSSF | 15238 | 06 | 037 | 186401 | 1 | 675 | 199 | 167 | 172 | 69 | 68 |
# list of additional columns to drop
columns_to_drop = ['Geo_GEOID','Geo_STUSAB','Geo_SUMLEV','Geo_GEOCOMP','Geo_FILEID','Geo_LOGRECNO','Geo_STATE','Geo_COUNTY','Geo_TRACT']
# next, drop them!
df = df.drop(columns_to_drop,axis=1)
df.head()
| Geo_FIPS | Geo_NAME | Geo_QName | Geo_BLKGRP | SE_B14001_001 | SE_B14001_002 | SE_B14001_003 | SE_B14001_004 | SE_B14001_005 | SE_B14001_006 | |
|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 060371863011 | Block Group 1, Census Tract 1863.01, Los Angel... | Block Group 1, Census Tract 1863.01, Los Angel... | 1 | 330 | 46 | 67 | 54 | 50 | 113 |
| 1 | 060371863012 | Block Group 2, Census Tract 1863.01, Los Angel... | Block Group 2, Census Tract 1863.01, Los Angel... | 2 | 327 | 95 | 148 | 31 | 9 | 44 |
| 2 | 060371863013 | Block Group 3, Census Tract 1863.01, Los Angel... | Block Group 3, Census Tract 1863.01, Los Angel... | 3 | 268 | 73 | 129 | 44 | 10 | 12 |
| 3 | 060371863021 | Block Group 1, Census Tract 1863.02, Los Angel... | Block Group 1, Census Tract 1863.02, Los Angel... | 1 | 584 | 129 | 115 | 109 | 71 | 160 |
| 4 | 060371864011 | Block Group 1, Census Tract 1864.01, Los Angel... | Block Group 1, Census Tract 1864.01, Los Angel... | 1 | 675 | 199 | 167 | 172 | 69 | 68 |
df.columns = ['FIPS',
'Geo_NAME',
'Geo_QName',
'Geo_BLKGRP',
'Total Households in Atwater Village',
'Household Income Less than $25,000',
'Household Income $25,000 to $49,999',
'Household Income $50,000 to $74,999',
'Household Income $75,000 to $99,999',
'Household Income $100,000 or More']
df.head()
| FIPS | Geo_NAME | Geo_QName | Geo_BLKGRP | Total Households in Atwater Village | Household Income Less than $25,000 | Household Income $25,000 to $49,999 | Household Income $50,000 to $74,999 | Household Income $75,000 to $99,999 | Household Income $100,000 or More | |
|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 060371863011 | Block Group 1, Census Tract 1863.01, Los Angel... | Block Group 1, Census Tract 1863.01, Los Angel... | 1 | 330 | 46 | 67 | 54 | 50 | 113 |
| 1 | 060371863012 | Block Group 2, Census Tract 1863.01, Los Angel... | Block Group 2, Census Tract 1863.01, Los Angel... | 2 | 327 | 95 | 148 | 31 | 9 | 44 |
| 2 | 060371863013 | Block Group 3, Census Tract 1863.01, Los Angel... | Block Group 3, Census Tract 1863.01, Los Angel... | 3 | 268 | 73 | 129 | 44 | 10 | 12 |
| 3 | 060371863021 | Block Group 1, Census Tract 1863.02, Los Angel... | Block Group 1, Census Tract 1863.02, Los Angel... | 1 | 584 | 129 | 115 | 109 | 71 | 160 |
| 4 | 060371864011 | Block Group 1, Census Tract 1864.01, Los Angel... | Block Group 1, Census Tract 1864.01, Los Angel... | 1 | 675 | 199 | 167 | 172 | 69 | 68 |
df_sum= df.sum().reset_index()
df_sum.head()
| index | 0 | |
|---|---|---|
| 0 | FIPS | 0603718630110603718630120603718630130603718630... |
| 1 | Geo_NAME | Block Group 1, Census Tract 1863.01, Los Angel... |
| 2 | Geo_QName | Block Group 1, Census Tract 1863.01, Los Angel... |
| 3 | Geo_BLKGRP | 31 |
| 4 | Total Households in Atwater Village | 8748 |
df_sum=df_sum.set_index('index').T
df_sum.head()
| index | FIPS | Geo_NAME | Geo_QName | Geo_BLKGRP | Total Households in Atwater Village | Household Income Less than $25,000 | Household Income $25,000 to $49,999 | Household Income $50,000 to $74,999 | Household Income $75,000 to $99,999 | Household Income $100,000 or More |
|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 0603718630110603718630120603718630130603718630... | Block Group 1, Census Tract 1863.01, Los Angel... | Block Group 1, Census Tract 1863.01, Los Angel... | 31 | 8748 | 1997 | 1879 | 1339 | 1157 | 2376 |
# create a new column and normalize for SUMA
# also repeat this for 'p_more_100k'
df_sum['% Less than $25K'] = df_sum['Household Income Less than $25,000']/df_sum['Total Households in Atwater Village']*100
df_sum['% More than $100K'] = df_sum['Household Income $100,000 or More']/df_sum['Total Households in Atwater Village']*100
df_sum['neighborhood'] = 'Atwater'
df_sum['year'] = '2017'
df_sum.plot.bar(x = 'neighborhood', y='% Less than $25K', title="Percentage of Households with Income Less than $25K (2017)")
<matplotlib.axes._subplots.AxesSubplot at 0x7f3dd4287670>
df_sum.plot.bar(x = 'neighborhood', y='% More than $100K', title="Percentage of Households with Income More than $100K (2017)")
<matplotlib.axes._subplots.AxesSubplot at 0x7f3dd42bcd30>
type(df_sum)
pandas.core.frame.DataFrame
df['Total Households in Atwater Village'].head()
0 330 1 327 2 268 3 584 4 675 Name: Total Households in Atwater Village, dtype: int64
# create a column to define the neighborhood
df['neighborhood'] = 'Atwater'
# create a new column and normalize
# also repeat this for 'p_more_100k'
df['p_less_25k'] = df['Household Income Less than $25,000']/df['Total Households in Atwater Village']*100
df['p_more_100k'] = df['Household Income $100,000 or More']/df['Total Households in Atwater Village']*100
df['Total Households'] = df ['Total Households in Atwater Village']
df.head()
| FIPS | Geo_NAME | Geo_QName | Geo_BLKGRP | Total Households in Atwater Village | Household Income Less than $25,000 | Household Income $25,000 to $49,999 | Household Income $50,000 to $74,999 | Household Income $75,000 to $99,999 | Household Income $100,000 or More | neighborhood | p_less_25k | p_more_100k | Total Households | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 060371863011 | Block Group 1, Census Tract 1863.01, Los Angel... | Block Group 1, Census Tract 1863.01, Los Angel... | 1 | 330 | 46 | 67 | 54 | 50 | 113 | Atwater | 13.939394 | 34.242424 | 330 |
| 1 | 060371863012 | Block Group 2, Census Tract 1863.01, Los Angel... | Block Group 2, Census Tract 1863.01, Los Angel... | 2 | 327 | 95 | 148 | 31 | 9 | 44 | Atwater | 29.051988 | 13.455657 | 327 |
| 2 | 060371863013 | Block Group 3, Census Tract 1863.01, Los Angel... | Block Group 3, Census Tract 1863.01, Los Angel... | 3 | 268 | 73 | 129 | 44 | 10 | 12 | Atwater | 27.238806 | 4.477612 | 268 |
| 3 | 060371863021 | Block Group 1, Census Tract 1863.02, Los Angel... | Block Group 1, Census Tract 1863.02, Los Angel... | 1 | 584 | 129 | 115 | 109 | 71 | 160 | Atwater | 22.089041 | 27.397260 | 584 |
| 4 | 060371864011 | Block Group 1, Census Tract 1864.01, Los Angel... | Block Group 1, Census Tract 1864.01, Los Angel... | 1 | 675 | 199 | 167 | 172 | 69 | 68 | Atwater | 29.481481 | 10.074074 | 675 |
import plotly.express as px
import pandas as pd
px.bar(df, x="neighborhood", y=["Household Income Less than $25,000",
"Household Income $25,000 to $49,999",
"Household Income $50,000 to $74,999",
"Household Income $75,000 to $99,999",
"Household Income $100,000 or More"], title="Income Breakdown by Block Group in Atwater Village (2017)",
labels={'Geo_NAME':'Census Tract Block Group','value':'Total Households in Atwater Village', 'variable': 'Household Income'})
df2 = pd.read_csv('data/Leimert2017_HHIncomeCategories.csv')
df2 = pd.read_csv(
'data/Leimert2017_HHIncomeCategories.csv',
dtype=
{
'Geo_FIPS':str,
'Geo_STATE':str,
'Geo_COUNTY': str
}
)
df2.columns[df2.isna().all()].tolist()
['Geo_US', 'Geo_REGION', 'Geo_DIVISION', 'Geo_STATECE', 'Geo_COUSUB', 'Geo_PLACE', 'Geo_PLACESE', 'Geo_CONCIT', 'Geo_AIANHH', 'Geo_AIANHHFP', 'Geo_AIHHTLI', 'Geo_AITSCE', 'Geo_AITS', 'Geo_ANRC', 'Geo_CBSA', 'Geo_CSA', 'Geo_METDIV', 'Geo_MACC', 'Geo_MEMI', 'Geo_NECTA', 'Geo_CNECTA', 'Geo_NECTADIV', 'Geo_UA', 'Geo_UACP', 'Geo_CDCURR', 'Geo_SLDU', 'Geo_SLDL', 'Geo_VTD', 'Geo_ZCTA3', 'Geo_ZCTA5', 'Geo_SUBMCD', 'Geo_SDELM', 'Geo_SDSEC', 'Geo_SDUNI', 'Geo_UR', 'Geo_PCI', 'Geo_TAZ', 'Geo_UGA', 'Geo_BTTR', 'Geo_BTBG', 'Geo_PUMA5', 'Geo_PUMA1']
df2 = df2.dropna(axis=1,how="all")
df2.head()
| Geo_FIPS | Geo_GEOID | Geo_NAME | Geo_QName | Geo_STUSAB | Geo_SUMLEV | Geo_GEOCOMP | Geo_FILEID | Geo_LOGRECNO | Geo_STATE | Geo_COUNTY | Geo_TRACT | Geo_BLKGRP | SE_B14001_001 | SE_B14001_002 | SE_B14001_003 | SE_B14001_004 | SE_B14001_005 | SE_B14001_006 | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 060372190203 | 15000US060372190203 | Block Group 3, Census Tract 2190.20, Los Angel... | Block Group 3, Census Tract 2190.20, Los Angel... | ca | 150 | 0 | ACSSF | 15883 | 06 | 037 | 219020 | 3 | 370 | 167 | 46 | 115 | 33 | 9 |
| 1 | 060372340001 | 15000US060372340001 | Block Group 1, Census Tract 2340, Los Angeles ... | Block Group 1, Census Tract 2340, Los Angeles ... | ca | 150 | 0 | ACSSF | 16104 | 06 | 037 | 234000 | 1 | 634 | 147 | 140 | 146 | 56 | 145 |
| 2 | 060372340002 | 15000US060372340002 | Block Group 2, Census Tract 2340, Los Angeles ... | Block Group 2, Census Tract 2340, Los Angeles ... | ca | 150 | 0 | ACSSF | 16105 | 06 | 037 | 234000 | 2 | 267 | 44 | 58 | 22 | 14 | 129 |
| 3 | 060372340003 | 15000US060372340003 | Block Group 3, Census Tract 2340, Los Angeles ... | Block Group 3, Census Tract 2340, Los Angeles ... | ca | 150 | 0 | ACSSF | 16106 | 06 | 037 | 234000 | 3 | 247 | 14 | 47 | 28 | 25 | 133 |
| 4 | 060372340004 | 15000US060372340004 | Block Group 4, Census Tract 2340, Los Angeles ... | Block Group 4, Census Tract 2340, Los Angeles ... | ca | 150 | 0 | ACSSF | 16107 | 06 | 037 | 234000 | 4 | 617 | 219 | 71 | 117 | 112 | 98 |
df2 = df2.drop(columns_to_drop,axis=1)
df2.head()
| Geo_FIPS | Geo_NAME | Geo_QName | Geo_BLKGRP | SE_B14001_001 | SE_B14001_002 | SE_B14001_003 | SE_B14001_004 | SE_B14001_005 | SE_B14001_006 | |
|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 060372190203 | Block Group 3, Census Tract 2190.20, Los Angel... | Block Group 3, Census Tract 2190.20, Los Angel... | 3 | 370 | 167 | 46 | 115 | 33 | 9 |
| 1 | 060372340001 | Block Group 1, Census Tract 2340, Los Angeles ... | Block Group 1, Census Tract 2340, Los Angeles ... | 1 | 634 | 147 | 140 | 146 | 56 | 145 |
| 2 | 060372340002 | Block Group 2, Census Tract 2340, Los Angeles ... | Block Group 2, Census Tract 2340, Los Angeles ... | 2 | 267 | 44 | 58 | 22 | 14 | 129 |
| 3 | 060372340003 | Block Group 3, Census Tract 2340, Los Angeles ... | Block Group 3, Census Tract 2340, Los Angeles ... | 3 | 247 | 14 | 47 | 28 | 25 | 133 |
| 4 | 060372340004 | Block Group 4, Census Tract 2340, Los Angeles ... | Block Group 4, Census Tract 2340, Los Angeles ... | 4 | 617 | 219 | 71 | 117 | 112 | 98 |
df2.columns = ['FIPS',
'Geo_NAME',
'Geo_QName',
'Geo_BLKGRP',
'Total Households in Leimert Park',
'Household Income Less than $25,000',
'Household Income $25,000 to $49,999',
'Household Income $50,000 to $74,999',
'Household Income $75,000 to $99,999',
'Household Income $100,000 or More']
df2.head()
| FIPS | Geo_NAME | Geo_QName | Geo_BLKGRP | Total Households in Leimert Park | Household Income Less than $25,000 | Household Income $25,000 to $49,999 | Household Income $50,000 to $74,999 | Household Income $75,000 to $99,999 | Household Income $100,000 or More | |
|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 060372190203 | Block Group 3, Census Tract 2190.20, Los Angel... | Block Group 3, Census Tract 2190.20, Los Angel... | 3 | 370 | 167 | 46 | 115 | 33 | 9 |
| 1 | 060372340001 | Block Group 1, Census Tract 2340, Los Angeles ... | Block Group 1, Census Tract 2340, Los Angeles ... | 1 | 634 | 147 | 140 | 146 | 56 | 145 |
| 2 | 060372340002 | Block Group 2, Census Tract 2340, Los Angeles ... | Block Group 2, Census Tract 2340, Los Angeles ... | 2 | 267 | 44 | 58 | 22 | 14 | 129 |
| 3 | 060372340003 | Block Group 3, Census Tract 2340, Los Angeles ... | Block Group 3, Census Tract 2340, Los Angeles ... | 3 | 247 | 14 | 47 | 28 | 25 | 133 |
| 4 | 060372340004 | Block Group 4, Census Tract 2340, Los Angeles ... | Block Group 4, Census Tract 2340, Los Angeles ... | 4 | 617 | 219 | 71 | 117 | 112 | 98 |
df2_sum= df2.sum().reset_index()
df2_sum=df2_sum.set_index('index').T
df2_sum.head()
| index | FIPS | Geo_NAME | Geo_QName | Geo_BLKGRP | Total Households in Leimert Park | Household Income Less than $25,000 | Household Income $25,000 to $49,999 | Household Income $50,000 to $74,999 | Household Income $75,000 to $99,999 | Household Income $100,000 or More |
|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 0603721902030603723400010603723400020603723400... | Block Group 3, Census Tract 2190.20, Los Angel... | Block Group 3, Census Tract 2190.20, Los Angel... | 34 | 5865 | 1885 | 1169 | 1051 | 560 | 1200 |
df2_sum['% Less than $25K'] = df2_sum['Household Income Less than $25,000']/df2_sum['Total Households in Leimert Park']*100
df2_sum['% More than $100K'] = df2_sum['Household Income $100,000 or More']/df2_sum['Total Households in Leimert Park']*100
df2_sum['neighborhood'] = 'Leimert'
df2_sum['year'] = '2017'
df2_sum.head()
| index | FIPS | Geo_NAME | Geo_QName | Geo_BLKGRP | Total Households in Leimert Park | Household Income Less than $25,000 | Household Income $25,000 to $49,999 | Household Income $50,000 to $74,999 | Household Income $75,000 to $99,999 | Household Income $100,000 or More | % Less than $25K | % More than $100K | neighborhood | year |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 0603721902030603723400010603723400020603723400... | Block Group 3, Census Tract 2190.20, Los Angel... | Block Group 3, Census Tract 2190.20, Los Angel... | 34 | 5865 | 1885 | 1169 | 1051 | 560 | 1200 | 32.1398 | 20.4604 | Leimert | 2017 |
# create a new column and normalize
# also repeat this for 'p_more_100k'
df2['p_less_25k'] = df2['Household Income Less than $25,000']/df2['Total Households in Leimert Park']*100
# create a column to define the neighborhood
df2['neighborhood'] = 'Leimert'
df2['p_more_100k'] = df2['Household Income $100,000 or More']/df2['Total Households in Leimert Park']*100
df2['Total Households'] = df2['Total Households in Leimert Park']
px.bar(df2, x="neighborhood", y=["Household Income Less than $25,000",
"Household Income $25,000 to $49,999",
"Household Income $50,000 to $74,999",
"Household Income $75,000 to $99,999",
"Household Income $100,000 or More"], title="Income Breakdown by Block Group in Leimert Park (2017)",
labels={'Geo_NAME':'Census Tract Block Group','value':'Total Households in Leimert Park', 'variable': 'Household Income'})
income_merged= df.append(df2)
incomefig2017 = px.bar(income_merged, x="neighborhood", y=["Household Income Less than $25,000",
"Household Income $25,000 to $49,999",
"Household Income $50,000 to $74,999",
"Household Income $75,000 to $99,999",
"Household Income $100,000 or More"], title="Income Breakdown by Neighborhood (2017)",
labels={'Geo_NAME':'Census Tract Block Group','value':'Total Households in Atwater Village & Leimert Park', 'variable': 'Household Income'})
incomefig2017.update_traces(marker_line_width=0)
incomefig2017.show()
incomefig2017.write_html("income2017_stacked_plotly.html")
income_merged= df_sum.append(df2_sum)
income_merged
| FIPS | Geo_NAME | Geo_QName | Geo_BLKGRP | Total Households in Atwater Village | Household Income Less than $25,000 | Household Income $25,000 to $49,999 | Household Income $50,000 to $74,999 | Household Income $75,000 to $99,999 | Household Income $100,000 or More | % Less than $25K | % More than $100K | neighborhood | year | Total Households in Leimert Park | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 0603718630110603718630120603718630130603718630... | Block Group 1, Census Tract 1863.01, Los Angel... | Block Group 1, Census Tract 1863.01, Los Angel... | 31 | 8748 | 1997 | 1879 | 1339 | 1157 | 2376 | 22.8281 | 27.1605 | Atwater | 2017 | NaN |
| 0 | 0603721902030603723400010603723400020603723400... | Block Group 3, Census Tract 2190.20, Los Angel... | Block Group 3, Census Tract 2190.20, Los Angel... | 34 | NaN | 1885 | 1169 | 1051 | 560 | 1200 | 32.1398 | 20.4604 | Leimert | 2017 | 5865 |
income_merged.plot.bar(x = 'neighborhood', y='% Less than $25K', title="Percentage of Households with Income Less than $25K (2017)")
<matplotlib.axes._subplots.AxesSubplot at 0x7f3dd1d18130>
income_merged.plot.bar(x = 'neighborhood', y='% More than $100K', title="Percentage of Households with Income More than $100K (2017)")
<matplotlib.axes._subplots.AxesSubplot at 0x7f3dd1cd27f0>
df3 = pd.read_csv('data/Atwater2017_Race.csv')
df3 = pd.read_csv(
'data/Atwater2017_Race.csv',
dtype=
{
'Geo_FIPS':str,
'Geo_STATE':str,
'Geo_COUNTY': str
}
)
df3.columns[df3.isna().all()].tolist()
['Geo_US', 'Geo_REGION', 'Geo_DIVISION', 'Geo_STATECE', 'Geo_COUSUB', 'Geo_PLACE', 'Geo_PLACESE', 'Geo_CONCIT', 'Geo_AIANHH', 'Geo_AIANHHFP', 'Geo_AIHHTLI', 'Geo_AITSCE', 'Geo_AITS', 'Geo_ANRC', 'Geo_CBSA', 'Geo_CSA', 'Geo_METDIV', 'Geo_MACC', 'Geo_MEMI', 'Geo_NECTA', 'Geo_CNECTA', 'Geo_NECTADIV', 'Geo_UA', 'Geo_UACP', 'Geo_CDCURR', 'Geo_SLDU', 'Geo_SLDL', 'Geo_VTD', 'Geo_ZCTA3', 'Geo_ZCTA5', 'Geo_SUBMCD', 'Geo_SDELM', 'Geo_SDSEC', 'Geo_SDUNI', 'Geo_UR', 'Geo_PCI', 'Geo_TAZ', 'Geo_UGA', 'Geo_BTTR', 'Geo_BTBG', 'Geo_PUMA5', 'Geo_PUMA1']
df3 = df3.dropna(axis=1,how="all")
columns_to_drop = ['Geo_GEOID','Geo_STUSAB','Geo_SUMLEV','Geo_GEOCOMP','Geo_FILEID','Geo_LOGRECNO','Geo_STATE', 'Geo_COUNTY','SE_A04001_002','SE_A04001_011','SE_A04001_012','SE_A04001_013','SE_A04001_014','SE_A04001_015','SE_A04001_016','SE_A04001_017']
df3 = df3.drop(columns_to_drop,axis=1)
df3.head()
| Geo_FIPS | Geo_NAME | Geo_QName | Geo_TRACT | Geo_BLKGRP | SE_A04001_001 | SE_A04001_003 | SE_A04001_004 | SE_A04001_005 | SE_A04001_006 | SE_A04001_007 | SE_A04001_008 | SE_A04001_009 | SE_A04001_010 | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 060371863011 | Block Group 1, Census Tract 1863.01, Los Angel... | Block Group 1, Census Tract 1863.01, Los Angel... | 186301 | 1 | 881 | 322 | 53 | 0 | 78 | 0 | 0 | 44 | 384 |
| 1 | 060371863012 | Block Group 2, Census Tract 1863.01, Los Angel... | Block Group 2, Census Tract 1863.01, Los Angel... | 186301 | 2 | 1349 | 103 | 23 | 0 | 89 | 0 | 0 | 6 | 1128 |
| 2 | 060371863013 | Block Group 3, Census Tract 1863.01, Los Angel... | Block Group 3, Census Tract 1863.01, Los Angel... | 186301 | 3 | 889 | 59 | 25 | 0 | 196 | 0 | 0 | 4 | 605 |
| 3 | 060371863021 | Block Group 1, Census Tract 1863.02, Los Angel... | Block Group 1, Census Tract 1863.02, Los Angel... | 186302 | 1 | 1573 | 566 | 26 | 0 | 409 | 0 | 0 | 84 | 488 |
| 4 | 060371864011 | Block Group 1, Census Tract 1864.01, Los Angel... | Block Group 1, Census Tract 1864.01, Los Angel... | 186401 | 1 | 2676 | 78 | 0 | 0 | 312 | 0 | 0 | 7 | 2279 |
columns3 = list(df3) # this is the same as df.columns.to_list()
columns3
['Geo_FIPS', 'Geo_NAME', 'Geo_QName', 'Geo_TRACT', 'Geo_BLKGRP', 'SE_A04001_001', 'SE_A04001_003', 'SE_A04001_004', 'SE_A04001_005', 'SE_A04001_006', 'SE_A04001_007', 'SE_A04001_008', 'SE_A04001_009', 'SE_A04001_010']
##RENAMED TO INCLUDE PROPER TITLES
df3.columns = ['FIPS',
'Geo_NAME',
'Geo_QName',
'Geo_TRACT',
'Geo_BLKGRP',
'Total Population',
'White',
'Black',
'AmIndian',
'Native Hawaiian and Other Pacific Islander Alone',
'Asian',
'Some Other Race Alone',
'Two or More Races',
'Hispanic or Latino']
df3.head()
| FIPS | Geo_NAME | Geo_QName | Geo_TRACT | Geo_BLKGRP | Total Population | White | Black | AmIndian | Native Hawaiian and Other Pacific Islander Alone | Asian | Some Other Race Alone | Two or More Races | Hispanic or Latino | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 060371863011 | Block Group 1, Census Tract 1863.01, Los Angel... | Block Group 1, Census Tract 1863.01, Los Angel... | 186301 | 1 | 881 | 322 | 53 | 0 | 78 | 0 | 0 | 44 | 384 |
| 1 | 060371863012 | Block Group 2, Census Tract 1863.01, Los Angel... | Block Group 2, Census Tract 1863.01, Los Angel... | 186301 | 2 | 1349 | 103 | 23 | 0 | 89 | 0 | 0 | 6 | 1128 |
| 2 | 060371863013 | Block Group 3, Census Tract 1863.01, Los Angel... | Block Group 3, Census Tract 1863.01, Los Angel... | 186301 | 3 | 889 | 59 | 25 | 0 | 196 | 0 | 0 | 4 | 605 |
| 3 | 060371863021 | Block Group 1, Census Tract 1863.02, Los Angel... | Block Group 1, Census Tract 1863.02, Los Angel... | 186302 | 1 | 1573 | 566 | 26 | 0 | 409 | 0 | 0 | 84 | 488 |
| 4 | 060371864011 | Block Group 1, Census Tract 1864.01, Los Angel... | Block Group 1, Census Tract 1864.01, Los Angel... | 186401 | 1 | 2676 | 78 | 0 | 0 | 312 | 0 | 0 | 7 | 2279 |
df3_sum= df3.sum().reset_index()
df3_sum
| index | 0 | |
|---|---|---|
| 0 | FIPS | 0603718630110603718630120603718630130603718630... |
| 1 | Geo_NAME | Block Group 1, Census Tract 1863.01, Los Angel... |
| 2 | Geo_QName | Block Group 1, Census Tract 1863.01, Los Angel... |
| 3 | Geo_TRACT | 3368419 |
| 4 | Geo_BLKGRP | 31 |
| 5 | Total Population | 25387 |
| 6 | White | 5639 |
| 7 | Black | 270 |
| 8 | AmIndian | 157 |
| 9 | Native Hawaiian and Other Pacific Islander Alone | 4663 |
| 10 | Asian | 66 |
| 11 | Some Other Race Alone | 48 |
| 12 | Two or More Races | 721 |
| 13 | Hispanic or Latino | 13823 |
df3_sum = df3_sum.set_index('index').T
type(df3_sum)
pandas.core.frame.DataFrame
df3_sum.head()
| index | FIPS | Geo_NAME | Geo_QName | Geo_TRACT | Geo_BLKGRP | Total Population | White | Black | AmIndian | Native Hawaiian and Other Pacific Islander Alone | Asian | Some Other Race Alone | Two or More Races | Hispanic or Latino |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 0603718630110603718630120603718630130603718630... | Block Group 1, Census Tract 1863.01, Los Angel... | Block Group 1, Census Tract 1863.01, Los Angel... | 3368419 | 31 | 25387 | 5639 | 270 | 157 | 4663 | 66 | 48 | 721 | 13823 |
df3_sum['% Latino'] = df3_sum['Hispanic or Latino']/df3_sum['Total Population']*100
df3_sum['% White'] = df3_sum['White']/df3_sum['Total Population']*100
df3_sum['% Black'] = df3_sum['Black']/df3_sum['Total Population']*100
df3_sum['neighborhood'] = 'Atwater'
df3_sum['year'] = '2017'
df3_sum.head()
| index | FIPS | Geo_NAME | Geo_QName | Geo_TRACT | Geo_BLKGRP | Total Population | White | Black | AmIndian | Native Hawaiian and Other Pacific Islander Alone | Asian | Some Other Race Alone | Two or More Races | Hispanic or Latino | % Latino | % White | % Black | neighborhood | year |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 0603718630110603718630120603718630130603718630... | Block Group 1, Census Tract 1863.01, Los Angel... | Block Group 1, Census Tract 1863.01, Los Angel... | 3368419 | 31 | 25387 | 5639 | 270 | 157 | 4663 | 66 | 48 | 721 | 13823 | 54.4491 | 22.2122 | 1.06354 | Atwater | 2017 |
df4 = pd.read_csv('data/Leimert2017_Race.csv')
df4 = pd.read_csv(
'data/Leimert2017_Race.csv',
dtype=
{
'Geo_FIPS':str,
'Geo_STATE':str,
'Geo_COUNTY': str
}
)
df4.columns[df4.isna().all()].tolist()
['Geo_US', 'Geo_REGION', 'Geo_DIVISION', 'Geo_STATECE', 'Geo_COUSUB', 'Geo_PLACE', 'Geo_PLACESE', 'Geo_CONCIT', 'Geo_AIANHH', 'Geo_AIANHHFP', 'Geo_AIHHTLI', 'Geo_AITSCE', 'Geo_AITS', 'Geo_ANRC', 'Geo_CBSA', 'Geo_CSA', 'Geo_METDIV', 'Geo_MACC', 'Geo_MEMI', 'Geo_NECTA', 'Geo_CNECTA', 'Geo_NECTADIV', 'Geo_UA', 'Geo_UACP', 'Geo_CDCURR', 'Geo_SLDU', 'Geo_SLDL', 'Geo_VTD', 'Geo_ZCTA3', 'Geo_ZCTA5', 'Geo_SUBMCD', 'Geo_SDELM', 'Geo_SDSEC', 'Geo_SDUNI', 'Geo_UR', 'Geo_PCI', 'Geo_TAZ', 'Geo_UGA', 'Geo_BTTR', 'Geo_BTBG', 'Geo_PUMA5', 'Geo_PUMA1']
df4 = df4.dropna(axis=1,how="all")
columns_to_drop = ['Geo_GEOID','Geo_STUSAB','Geo_SUMLEV','Geo_GEOCOMP','Geo_FILEID','Geo_LOGRECNO','Geo_STATE', 'Geo_COUNTY','SE_A04001_002','SE_A04001_011','SE_A04001_012','SE_A04001_013','SE_A04001_014','SE_A04001_015','SE_A04001_016','SE_A04001_017']
df4 = df4.drop(columns_to_drop,axis=1)
df4.head()
| Geo_FIPS | Geo_NAME | Geo_QName | Geo_TRACT | Geo_BLKGRP | SE_A04001_001 | SE_A04001_003 | SE_A04001_004 | SE_A04001_005 | SE_A04001_006 | SE_A04001_007 | SE_A04001_008 | SE_A04001_009 | SE_A04001_010 | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 060372190203 | Block Group 3, Census Tract 2190.20, Los Angel... | Block Group 3, Census Tract 2190.20, Los Angel... | 219020 | 3 | 1074 | 10 | 480 | 0 | 33 | 0 | 0 | 0 | 551 |
| 1 | 060372340001 | Block Group 1, Census Tract 2340, Los Angeles ... | Block Group 1, Census Tract 2340, Los Angeles ... | 234000 | 1 | 1849 | 7 | 849 | 0 | 0 | 0 | 0 | 45 | 948 |
| 2 | 060372340002 | Block Group 2, Census Tract 2340, Los Angeles ... | Block Group 2, Census Tract 2340, Los Angeles ... | 234000 | 2 | 559 | 59 | 428 | 0 | 9 | 0 | 0 | 11 | 52 |
| 3 | 060372340003 | Block Group 3, Census Tract 2340, Los Angeles ... | Block Group 3, Census Tract 2340, Los Angeles ... | 234000 | 3 | 697 | 41 | 606 | 0 | 23 | 0 | 0 | 19 | 8 |
| 4 | 060372340004 | Block Group 4, Census Tract 2340, Los Angeles ... | Block Group 4, Census Tract 2340, Los Angeles ... | 234000 | 4 | 1267 | 128 | 852 | 0 | 6 | 0 | 0 | 45 | 236 |
columns4 = list(df4) # this is the same as df.columns.to_list()
columns4
['Geo_FIPS', 'Geo_NAME', 'Geo_QName', 'Geo_TRACT', 'Geo_BLKGRP', 'SE_A04001_001', 'SE_A04001_003', 'SE_A04001_004', 'SE_A04001_005', 'SE_A04001_006', 'SE_A04001_007', 'SE_A04001_008', 'SE_A04001_009', 'SE_A04001_010']
df4.columns = ['FIPS',
'Geo_NAME',
'Geo_QName',
'Geo_TRACT',
'Geo_BLKGRP',
'Total Population',
'White',
'Black',
'AmIndian',
'Native Hawaiian and Other Pacific Islander Alone',
'Asian',
'Some Other Race Alone',
'Two or More Races',
'Hispanic or Latino']
df4.head()
| FIPS | Geo_NAME | Geo_QName | Geo_TRACT | Geo_BLKGRP | Total Population | White | Black | AmIndian | Native Hawaiian and Other Pacific Islander Alone | Asian | Some Other Race Alone | Two or More Races | Hispanic or Latino | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 060372190203 | Block Group 3, Census Tract 2190.20, Los Angel... | Block Group 3, Census Tract 2190.20, Los Angel... | 219020 | 3 | 1074 | 10 | 480 | 0 | 33 | 0 | 0 | 0 | 551 |
| 1 | 060372340001 | Block Group 1, Census Tract 2340, Los Angeles ... | Block Group 1, Census Tract 2340, Los Angeles ... | 234000 | 1 | 1849 | 7 | 849 | 0 | 0 | 0 | 0 | 45 | 948 |
| 2 | 060372340002 | Block Group 2, Census Tract 2340, Los Angeles ... | Block Group 2, Census Tract 2340, Los Angeles ... | 234000 | 2 | 559 | 59 | 428 | 0 | 9 | 0 | 0 | 11 | 52 |
| 3 | 060372340003 | Block Group 3, Census Tract 2340, Los Angeles ... | Block Group 3, Census Tract 2340, Los Angeles ... | 234000 | 3 | 697 | 41 | 606 | 0 | 23 | 0 | 0 | 19 | 8 |
| 4 | 060372340004 | Block Group 4, Census Tract 2340, Los Angeles ... | Block Group 4, Census Tract 2340, Los Angeles ... | 234000 | 4 | 1267 | 128 | 852 | 0 | 6 | 0 | 0 | 45 | 236 |
df4_sum= df4.sum().reset_index()
df4_sum=df4_sum.set_index('index').T
type(df4_sum)
pandas.core.frame.DataFrame
df4_sum.head()
| index | FIPS | Geo_NAME | Geo_QName | Geo_TRACT | Geo_BLKGRP | Total Population | White | Black | AmIndian | Native Hawaiian and Other Pacific Islander Alone | Asian | Some Other Race Alone | Two or More Races | Hispanic or Latino |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 0603721902030603723400010603723400020603723400... | Block Group 3, Census Tract 2190.20, Los Angel... | Block Group 3, Census Tract 2190.20, Los Angel... | 3028820 | 34 | 12623 | 552 | 8641 | 0 | 412 | 9 | 10 | 425 | 2574 |
df4_sum['% Black'] = (df4_sum['Black']/df4_sum['Total Population'])*100
df4_sum['% White'] = (df4_sum['White']/df4_sum['Total Population'])*100
df4_sum['% Latino'] = (df4_sum['Hispanic or Latino']/df4_sum['Total Population'])*100
df4_sum.head()
| index | FIPS | Geo_NAME | Geo_QName | Geo_TRACT | Geo_BLKGRP | Total Population | White | Black | AmIndian | Native Hawaiian and Other Pacific Islander Alone | Asian | Some Other Race Alone | Two or More Races | Hispanic or Latino | % Black | % White | % Latino |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 0603721902030603723400010603723400020603723400... | Block Group 3, Census Tract 2190.20, Los Angel... | Block Group 3, Census Tract 2190.20, Los Angel... | 3028820 | 34 | 12623 | 552 | 8641 | 0 | 412 | 9 | 10 | 425 | 2574 | 68.4544 | 4.37297 | 20.3913 |
df4_sum['neighborhood'] = 'Leimert'
df4_sum['year'] = '2017'
df4_sum
| index | FIPS | Geo_NAME | Geo_QName | Geo_TRACT | Geo_BLKGRP | Total Population | White | Black | AmIndian | Native Hawaiian and Other Pacific Islander Alone | Asian | Some Other Race Alone | Two or More Races | Hispanic or Latino | % Black | % White | % Latino | neighborhood | year |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 0603721902030603723400010603723400020603723400... | Block Group 3, Census Tract 2190.20, Los Angel... | Block Group 3, Census Tract 2190.20, Los Angel... | 3028820 | 34 | 12623 | 552 | 8641 | 0 | 412 | 9 | 10 | 425 | 2574 | 68.4544 | 4.37297 | 20.3913 | Leimert | 2017 |
race_merged= df3_sum.append(df4_sum)
race_merged
| FIPS | Geo_NAME | Geo_QName | Geo_TRACT | Geo_BLKGRP | Total Population | White | Black | AmIndian | Native Hawaiian and Other Pacific Islander Alone | Asian | Some Other Race Alone | Two or More Races | Hispanic or Latino | % Latino | % White | % Black | neighborhood | year | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 0603718630110603718630120603718630130603718630... | Block Group 1, Census Tract 1863.01, Los Angel... | Block Group 1, Census Tract 1863.01, Los Angel... | 3368419 | 31 | 25387 | 5639 | 270 | 157 | 4663 | 66 | 48 | 721 | 13823 | 54.4491 | 22.2122 | 1.06354 | Atwater | 2017 |
| 0 | 0603721902030603723400010603723400020603723400... | Block Group 3, Census Tract 2190.20, Los Angel... | Block Group 3, Census Tract 2190.20, Los Angel... | 3028820 | 34 | 12623 | 552 | 8641 | 0 | 412 | 9 | 10 | 425 | 2574 | 20.3913 | 4.37297 | 68.4544 | Leimert | 2017 |
race_merged.plot.bar(x = 'neighborhood', y='% White', title="Percentage of White Population (2017)")
<matplotlib.axes._subplots.AxesSubplot at 0x7f3dd1c9f9a0>
race_merged.plot.bar(x = 'neighborhood', y='% Black', title="Percentage of Black Population (2017)")
<matplotlib.axes._subplots.AxesSubplot at 0x7f3dd1b7d520>
race_merged.plot.bar(x = 'neighborhood', y='% Latino', title="Percentage of Latino Population (2017)")
<matplotlib.axes._subplots.AxesSubplot at 0x7f3dd1b54dc0>
racefig2017 = px.bar(race_merged, x="neighborhood", y=["White",
"Black",
"AmIndian",
"Native Hawaiian and Other Pacific Islander Alone",
"Asian", "Some Other Race Alone",'Two or More Races','Hispanic or Latino'], title="Income Breakdown by Neighborhood (2017)",
labels={'Geo_NAME':'Census Tract Block Group','value':'Total Households in Atwater Village & Leimert Park', 'variable': 'Household Income'})
racefig2017
import geopandas as gpd
block_groups= gpd.read_file('data/tl_2019_06_bg.shp')
# subset to only LA block groups
block_groups_LA = block_groups[block_groups.COUNTYFP == '037']
block_groups_LA.shape
(6425, 13)
columns = list(block_groups)
columns
['STATEFP', 'COUNTYFP', 'TRACTCE', 'BLKGRPCE', 'GEOID', 'NAMELSAD', 'MTFCC', 'FUNCSTAT', 'ALAND', 'AWATER', 'INTPTLAT', 'INTPTLON', 'geometry']
blocks = block_groups_LA[['GEOID','geometry']]
blocks.head()
| GEOID | geometry | |
|---|---|---|
| 30 | 060371872002 | POLYGON ((-118.25704 34.10773, -118.25664 34.1... |
| 31 | 060371873001 | POLYGON ((-118.26544 34.11216, -118.26540 34.1... |
| 32 | 060371873002 | POLYGON ((-118.25924 34.09416, -118.25924 34.0... |
| 36 | 060375437024 | POLYGON ((-118.27553 33.80936, -118.27553 33.8... |
| 37 | 060375437025 | POLYGON ((-118.27549 33.81246, -118.27549 33.8... |
blocks.columns = ['FIPS','geometry']
# create a new dataframe based on the join (ATWATER)
blocks_units=blocks.merge(df,on="FIPS")
# subsetting by neighborhood
axA1=blocks_units[blocks_units.neighborhood=='Atwater'].plot(figsize=(12,10),
column='p_less_25k',
cmap='cool',
legend=True,
scheme='equal_interval')
# get rid of the axis
axA1.axis('off')
# add a title
axA1.set_title('Census Block Groups in Atwater Village with Annual Household Income Less than $25K (2017)')
Text(0.5, 1.0, 'Census Block Groups in Atwater Village with Annual Household Income Less than $25K (2017)')
# create a new dataframe based on the join (Leimert)
blocks_units2=blocks.merge(df2,on="FIPS")
# subsetting by neighborhood
axL1= blocks_units2[blocks_units2.neighborhood=='Leimert'].plot(figsize=(12,10),
column='p_less_25k',
cmap='cool',
legend=True,
scheme='equal_interval')
# get rid of the axis
axL1.axis('off')
# add a title
axL1.set_title('Census Block Groups in Leimert Park with Annual Household Income Less than $25K (2017)')
Text(0.5, 1.0, 'Census Block Groups in Leimert Park with Annual Household Income Less than $25K (2017)')
# subsetting by neighborhood
axA2=blocks_units[blocks_units.neighborhood=='Atwater'].plot(figsize=(12,10),
column='p_more_100k',
cmap='cool',
legend=True,
scheme='equal_interval')
# get rid of the axis
axA2.axis('off')
# add a title
axA2.set_title('Census Block Groups in Atwater Village with Annual Household Income More than $100K (2017)')
Text(0.5, 1.0, 'Census Block Groups in Atwater Village with Annual Household Income More than $100K (2017)')
# subsetting by neighborhood
axL2 = blocks_units2[blocks_units2.neighborhood=='Leimert'].plot(figsize=(12,10),
column='p_more_100k',
cmap='cool',
legend=True,
scheme='equal_interval')
# get rid of the axis
axL2.axis('off')
# add a title
axL2.set_title('Census Block Groups in Leimert Park with Annual Household Income More than $100K (2017)')
Text(0.5, 1.0, 'Census Block Groups in Leimert Park with Annual Household Income More than $100K (2017)')
import folium
m = folium.Map(location=[34.2,-118.2],
zoom_start = 9,
tiles='CartoDB positron',
attribution='CartoDB')
# plot chorpleth over the base map
folium.Choropleth(
geo_data=blocks_units, # geo data
data=blocks_units, # data
key_on='feature.properties.FIPS', # key, or merge column
columns=['FIPS', 'p_more_100k'], # [key, value]
fill_color='BuPu',
line_weight=0.1,
fill_opacity=0.8,
line_opacity=0.2, # line opacity (of the border)
legend_name='Income X').add_to(m) # name on the legend color bar
m
m2 = folium.Map(location=[34.2,-118.2],
zoom_start = 9,
tiles='CartoDB positron',
attribution='CartoDB')
# plot chorpleth over the base map
folium.Choropleth(
geo_data=blocks_units2, # geo data
data=blocks_units2, # data
key_on='feature.properties.FIPS', # key, or merge column
columns=['FIPS', 'p_more_100k'], # [key, value]
fill_color='BuPu',
line_weight=0.1,
fill_opacity=0.8,
line_opacity=0.2, # line opacity (of the border)
legend_name='Income X').add_to(m2) # name on the legend color bar
m2
import pandas as pd
import plotly.express as px
from sodapy import Socrata
import geopandas as gpd
# connect to the data portal
client = Socrata("data.lacity.org", None)
# First 2000 results, returned as JSON from API / converted to Python list of
# dictionaries by sodapy.
results = client.get("hyem-e7yr",
limit=25000,
where = "issue_date between '2017-07-01T00:00:00' and '2020-11-30T00:00:00'",
order='issue_date desc')
# Convert to pandas DataFrame
adu = pd.DataFrame.from_records(results)
# print it with .sample, which gives you random rows
adu.head()
WARNING:root:Requests made without an app_token will be subject to strict throttling limits.
| zip_code | address_end | floor_area_l_a_building_code_definition | work_description | reference_old_permit | census_tract | permit_category | latest_status | initiating_office | assessor_parcel | ... | address_fraction_end | applicant_address_2 | block | location_1 | of_residential_dwelling_units | applicant_business_name | unit_range_start | unit_range_end | occupancy | suffix_direction | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 90047 | 10208 | 111 | ePlan Adu. CONVERT EXISTING DETACHED GARAGE IN... | 20ON 2243 | 2380.00 | Plan Check | Issued | METRO | 012 | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| 1 | 90008 | 3832 | 0 | Convert (E) detached garage to ADU per CA Govt... | 20ON 2233 | 2342.00 | Plan Check | Issued | METRO | 012 | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| 2 | 90016 | 5454 | 1245.5 | NEW TWO STORY ADU PER ORDINANCE WITH ATTACHED... | 20ON 2236 | 2199.01 | Plan Check | Issued | WEST LA | 011 | ... | 1/2 | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| 3 | 90065 | 1000 | 0 | Grading for (N) ADU Conversion and (N) office/... | 20ON 2238 | 1862.03 | Plan Check | Issued | METRO | 030 | ... | NaN | A | 25 | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
| 4 | 90064 | 2833 | 545 | CONVERT (E) DETACHED GARAGE INTO ACCESSORY DWE... | 20ON 2233 | 2713.00 | Plan Check | Issued | WEST LA | 006 | ... | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN | NaN |
5 rows × 53 columns
adu.shape
(2096, 53)
list(adu)
['zip_code', 'address_end', 'floor_area_l_a_building_code_definition', 'work_description', 'reference_old_permit', 'census_tract', 'permit_category', 'latest_status', 'initiating_office', 'assessor_parcel', 'applicant_first_name', 'zone', 'assessor_book', 'applicant_address_1', 'license_expiration_date', 'license_type', 'valuation', 'pcis_permit', 'applicant_relationship', 'address_start', 'street_name', 'street_suffix', 'street_direction', 'applicant_address_3', 'status_date', 'applicant_last_name', 'license', 'floor_area_l_a_zoning_code_definition', 'issue_date', 'assessor_page', 'contractors_business_name', 'tract', 'lot', 'permit_sub_type', 'of_stories', 'permit_type', 'principal_first_name', 'contractor_state', 'principal_middle_name', 'contractor_city', 'principal_last_name', 'contractor_address', 'address_fraction_start', 'address_fraction_end', 'applicant_address_2', 'block', 'location_1', 'of_residential_dwelling_units', 'applicant_business_name', 'unit_range_start', 'unit_range_end', 'occupancy', 'suffix_direction']
adu = gpd.read_file('https://data.lacity.org/resource/hyem-e7yr.geojson')
adu.head()
| assessor_parcel | zip_code | location_1_address | work_description | :@computed_region_2dna_qi2s | applicant_address_3 | floor_area_l_a_zoning_code_definition | address_fraction_end | project_number | suffix_direction | ... | event_code | reference_old_permit | applicant_relationship | :@computed_region_k96s_3jcv | contractor_state | license_expiration_date | :@computed_region_qz3q_ghft | applicant_address_2 | permit_sub_type | geometry | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 034 | 91367 | None | NEW FIRE SPRINKLER SYSTEM FOR ADU PER NFPA 13... | None | ARLETA, CA | None | None | None | None | ... | None | None | Contractor | None | CA | 2021-06-30T00:00:00 | None | None | 1 or 2 Family Dwelling | None |
| 1 | 046 | 91316 | None | NFPA13D FOR ADU. EXISTING 1'' DOMESTIC WATER ... | None | SUN VALLEY, CA | None | None | None | None | ... | None | None | Contractor | None | CA | 2021-12-31T00:00:00 | None | UNIT G | 1 or 2 Family Dwelling | None |
| 2 | 024 | 90025 | None | NFPA 13D SYSTEM . 1" DOMESTIC METER SEVRVES TH... | None | None | None | None | None | None | ... | None | None | Contractor | None | CA | 2021-10-31T00:00:00 | None | None | 1 or 2 Family Dwelling | None |
| 3 | 014 | 90034 | None | NEW FIRE SPRINKLER SYSTEM FOR PER NFPA 13D FOR... | None | WOODLAND HILLS, CA | None | None | None | None | ... | None | None | Contractor | None | CA | 2021-01-31T00:00:00 | None | None | 1 or 2 Family Dwelling | None |
| 4 | 027 | 91436 | New fire sprinkler system for ADU per NFPA-13D... | 62 | WEST HILLS,CA | None | None | None | None | ... | None | None | Agent for Contractor | 327 | CA | 2021-10-31T00:00:00 | 19737 | None | 1 or 2 Family Dwelling | POINT (-118.49822 34.14598) |
5 rows × 65 columns
adu = adu[['issue_date','geometry']]
# print it with .sample, which gives you random rows
adu.head()
| issue_date | geometry | |
|---|---|---|
| 0 | 2020-12-03T00:00:00 | None |
| 1 | 2020-10-30T00:00:00 | None |
| 2 | 2020-10-27T00:00:00 | None |
| 3 | 2020-09-30T00:00:00 | None |
| 4 | 2020-09-18T00:00:00 | POINT (-118.49822 34.14598) |
#We can a latitude and longtitude column so that we can map it
adu['x'] = adu.geometry.x
adu['y'] = adu.geometry.y
adu = adu.dropna()
# get the layers into a web mercator projection
# reproject to web mercator
adu = adu.to_crs('EPSG:3857')
# map it!
adu.plot(figsize=(12,12),color='teal')
<matplotlib.axes._subplots.AxesSubplot at 0x7f3dbcceaac0>
adu = adu.to_crs('EPSG:3857')
blocks_units= blocks_units.to_crs('EPSG:3857')
# subset the neighborhoods geodataframe for a single neighborhood
neighborhood = blocks_units[blocks_units.neighborhood=='Atwater']
# use the bounding box coordinates to set the x and y limits
minx, miny, maxx, maxy = neighborhood.geometry.total_bounds
# do a spatial join to get crime in neighborhood
adus_in_neighborhood = gpd.sjoin(adu,neighborhood,how='inner')
# define the base layer to be the neighborhood polygon
base = neighborhood.plot(figsize=(12,12),
color='red',
edgecolor='red',
alpha=0.1)
# add the crime data, making sure to add the neighborhood polygon
ax = adus_in_neighborhood.plot(ax=base,
# column='applicant_address_3',
markersize=40,
legend=True,
cmap='tab20',
legend_kwds={
'loc': 'upper right',
'bbox_to_anchor':(1.3,1)
} # this puts the legend to the side
)
# set the map extent to the extent of the neighborhood bounds
ax.set_xlim(minx - 200, maxx + 200) # added/substracted value is to give some margin around total bounds
ax.set_ylim(miny - 200, maxy + 200)
# turn off the axis
ax.axis('off')
# add a title
ax.set_title('ADUs Constructed in Los Angeles',fontsize=20)
# add a basemap
ctx.add_basemap(m2,source=ctx.providers.CartoDB.Positron)
ax
--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-198-dc71909b008f> in <module> 38 39 # add a basemap ---> 40 ctx.add_basemap(m2,source=ctx.providers.CartoDB.Positron) 41 ax NameError: name 'ctx' is not defined
neighborhood.crs